/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.saleticket.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.MessageUtil;
import com.lightkits.common.db.entity.BdTag;
import com.lightkits.common.db.entity.FndWarehouse;
import com.lightkits.common.db.entity.WmSalesTicket;
import com.lightkits.common.db.entity.category.FndCategoryBase;
import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.common.pipeline.session.SessionInfo;
import com.lightkits.wms.database.dao.WmsBdTagMapper;
import com.lightkits.wms.database.model.BdComTagModel;
import com.lightkits.wms.domain.saleticket.QuerySaleMesService;
import com.lightkits.wms.domain.saleticket.model.SaleConstrant;
import com.lightkits.wms.dto.saleticket.model.ComSaleTicketDto;
import com.lightkits.wms.dto.saleticket.model.SimSaleTicketDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jing.wei@hand-china.com
 * @date 2019/6/28 11:02
 **/
@Service
public class QuerySaleMesServiceImpl implements QuerySaleMesService {
    private final WmsBdTagMapper tagMapper;

    @Autowired
    public QuerySaleMesServiceImpl(WmsBdTagMapper tagMapper) {
        this.tagMapper = tagMapper;
    }

    /**
     * 获取简单的售货单信息
     *
     * @param saleTicketNum 售货单编号
     * @return SimSaleTicketDto
     */
    @Override
    public List<SimSaleTicketDto> getSimSaleTicket(String saleTicketNum, String orgId) {
        List<SimSaleTicketDto> dtos = new ArrayList<>();
        List<WmSalesTicket> salesTickets = new WmSalesTicket().selectList(
                new EntityWrapper().like(WmSalesTicket.SALES_TICKEET_NUM, saleTicketNum)
                        .eq(WmSalesTicket.SO_STATUS, SaleConstrant.SALE_TICKET_STATUS)
                        .eq(WmSalesTicket.ORGANIZATION_ID, orgId));
        if (ObjectUtil.isNullOrEmpty(salesTickets)) {
            return dtos;
        }
        salesTickets.forEach(target -> {
            SimSaleTicketDto simDto = new SimSaleTicketDto();
            simDto.setCustomerAddress(target.getCustomerAddress());
            simDto.setCustomerPhone(target.getCustomerPhone());
            simDto.setSaleTicketId(target.getId().toString());
            simDto.setBookMoney(target.getBookedMoney().toString());
            simDto.setSaleTicketNum(target.getSalesTickeetNum());
            simDto.setCustomerName(target.getCustomerName());
            dtos.add(simDto);
        });
        return dtos;
    }

    /**
     * 获取条码信息
     *
     * @param ticketId 售货单Id
     * @param orgId    组织ID
     * @param tagCode  条码编码
     * @return ComSaleTicketDto
     */
    @Override
    public List<ComSaleTicketDto> getComSaleTicket(String ticketId,
                                                   String orgId,
                                                   String tagCode) {
        List<ComSaleTicketDto> dtos = new ArrayList<>();
        List<BdComTagModel> models = tagMapper.getTagListBySale(ticketId, orgId, tagCode, SessionInfo.languageCode());
        if (ObjectUtil.isNullOrEmpty(models)) {
            return dtos;
        }
        FndCategoryBase categoryBase = new FndCategoryBase().selectOne(new EntityWrapper()
                .eq(FndCategoryBase.ORGANIZATION_ID, orgId)
                .eq(FndCategoryBase.CATEGORY_CODE, SaleConstrant.SALE_TICKET_STATUS));
        List<FndWarehouse> warehouse = new FndWarehouse()
                .selectList(new EntityWrapper()
                        .eq(FndWarehouse.ORGANIZATION_ID, orgId)
                        .eq(FndWarehouse.ENABLED_FLAG, true)
                        .eq(FndWarehouse.WAREHOUSE_CATEGORY_ID, categoryBase.getId()));
        if (ObjectUtil.isEmpty(warehouse)) {
            throw new LightKitsBizException((MessageUtil.getSysMessage("MUST_HAVE_BOOKED_WH")));
        }
        for (BdComTagModel model : models) {
            ComSaleTicketDto dto = new ComSaleTicketDto();
            if (ObjectUtil.isNullOrEmpty(model.getStoreWarehouseId())) {
                throw new LightKitsBizException(MessageUtil.getSysMessage("MUST_HAVE_SALE_WM"));
            }

            if (!ObjectUtil.isNullOrEmpty(tagCode)
                    && warehouse.get(0).getId().toString().equals(model.getFromWarehouseId())) {
                throw new LightKitsBizException(MessageUtil.getSysMessage("TAG_CANNONT_BELONG_BOOKED_WM"));
            }
            BeanUtils.copyProperties(model, dto);
            dto.setLineStatus(SaleConstrant.SALE_TICKET_STATUS);
            dtos.add(dto);
        }
        return dtos;
    }

    /**
     * 获取条码信息
     *
     * @param bookStatus 是否校验定金仓
     * @param orgId      组织ID
     * @param tagCode    条码编码
     * @return ComSaleTicketDto
     */
    @Override
    public ComSaleTicketDto getComSaleTicket(Boolean bookStatus,
                                             String orgId,
                                             String tagCode) {
        if (ObjectUtil.isNullOrEmpty(orgId) || ObjectUtil.isNullOrEmpty(tagCode)) {
            throw new LightKitsBizException("Params is Null");
        }
        BdTag tag = new BdTag().selectOne(new EntityWrapper()
                .eq(BdTag.TAG_CODE, tagCode)
                .eq(BdTag.OWNER_ORGANIZATION_ID, orgId));
        if (tag == null || "END".equals(tag.getTagStatus())) {
            throw new LightKitsBizException(
                    MessageUtil.getSysMessage("TAG_NOT_EXSIT_OR_CANNOT_UESD"));
        }
        ComSaleTicketDto dtos = new ComSaleTicketDto();
        BdComTagModel models = tagMapper.getTagBySale(orgId, tagCode, SessionInfo.languageCode());
        if (ObjectUtil.isEmpty(models)) {
            return dtos;
        }
        FndCategoryBase categoryBase = new FndCategoryBase().selectOne(new EntityWrapper()
                .eq(FndCategoryBase.ORGANIZATION_ID, orgId)
                .eq(FndCategoryBase.CATEGORY_CODE, SaleConstrant.SALE_TICKET_STATUS));
        List<FndWarehouse> warehouse = new FndWarehouse()
                .selectList(new EntityWrapper()
                        .eq(FndWarehouse.ORGANIZATION_ID, orgId)
                        .eq(FndWarehouse.ENABLED_FLAG, true)
                        .eq(FndWarehouse.WAREHOUSE_CATEGORY_ID, categoryBase.getId()));
        if (ObjectUtil.isEmpty(warehouse)) {
            throw new LightKitsBizException(MessageUtil.getSysMessage("MUST_HAVE_BOOKED_WH"));
        }
        if (bookStatus
                && warehouse.get(0).getId().toString().equals(models.getFromWarehouseId())) {
            throw new LightKitsBizException(MessageUtil.getSysMessage("TAG_CANNONT_BELONG_BOOKED_WM"));
        }
        BeanUtils.copyProperties(models, dtos);
        return dtos;
    }
}
